$(function() {
	soillayerInfo();
//	$.ajax({
//		url : "findmapserver",
//		dataType : "json",
//		type : "POST",
//		data : {},
//		async : false,
//		error : function(error) {
//			console.log(error.responseText);
//
//			$.gritter.add({
//				title : '服务器错误',
//				text : '对不起，服务器错误，请联系系统管理员！',
//				class_name : 'gritter-error'
//			});
//
//		},
//		success : function(strValue) {
//			if (strValue != null) {
//
//				$("#soil_gis_mapserver").empty();
//
//				for ( var i in strValue) {
//					var option = "<option value=" + strValue[i] + ">"
//							+ strValue[i] + "</option>";
//					$("#soil_gis_mapserver").append(option);
//				}
//
//			}
//		}
//	});

	$('#socioEconomic_form')
			.validate(
					{
						errorElement : 'div',
						errorClass : 'help-block',
						focusInvalid : true,
						ignore : "",
						rules : {
							name : {
								required : true,
								maxlength : 100
							},
							official_area : {
								required : true,
								maxlength : 100
							},
							// gis_area : {
							// required : true,
							// maxlength : 100
							// },
							location_gis_mapserver : {
								required : true,
								maxlength : 100
							},
							location_gis_layer : {
								required : true,
								maxlength : 100

							},
							location_des : {
								required : true,
								maxlength : 100
							},
							border_des : {
								required : true,
								maxlength : 100
							}
						},

						messages : {
							name : {
								required : "<span style='color: red;'>请输入名称！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							},
							official_area : {
								required : "<span style='color: red;'>请输入官方面积！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							},
							// gis_area : {
							// required : "<span style='color:
							// red;'>请输入一些描述！</span>",
							// maxlength : "<span style='color:
							// red;'>最大长度不能超过500个字符！</span>"
							// },
							location_gis_mapserver : {
								required : "<span style='color: red;'>请选择Mapserver！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							},
							location_gis_layer : {
								required : "<span style='color: red;'>请选择图层！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							},
							location_des : {
								required : "<span style='color: red;'>请输入地理区位！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							},
							border_des : {
								required : "<span style='color: red;'>请输入边界描述！</span>",
								maxlength : "<span style='color: red;'>最大长度不能超过100个字符！</span>"
							}
						},

						highlight : function(e) {
							$(e).closest('.form-group').removeClass('has-info')
									.addClass('has-error');
							basicInfo.html(basicInfo.text() + warnHTML);
						},

						success : function(e) {
							$(e).closest('.form-group')
									.removeClass('has-error').addClass(
											'has-info');
							// $(e).remove();
						}
					});

	$("#socioEconomic_btn").click(function() {
		socioEconomic_add();
	});
	geteconomicIndicator();
});

function soillayerInfo() {
	
var id =$("#wetlandParkId").val();
	
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				$("#soil_gis_mapserver").empty();
				$("#soil_gis_mapserver").append("<option selected='selected' value=" + strValue.wlin_gis_mapserver + ">"
						+ strValue.wlin_gis_mapserver + "</option>");
			}
		}
	});
	
	
	
	dojo.require("esri.map");
	$("#soil_gis_layer").empty();
	var layer = [];
	var mapserver = $("#soil_gis_mapserver").val();

	$.ajax({
		url : "findmapserver",
		dataType : "json",
		type : "POST",
		data : {mapserver:mapserver},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {

				$("#soil_gis_layer").empty();

				for ( var i in strValue) {
					var option = "<option value=" + strValue[i] + ">"
							+ strValue[i] + "</option>";
					$("#soil_gis_layer").append(option);
				}

			}
		}
	});

}

function geteconomicIndicator(){
	var id =$("#wetlandParkId").val();
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				$("#soil_gis_layer").find("option[value='"+strValue.wlin_soil_gis_layer+"']").attr("selected",true);
				var arr = $("input[name='content']");
				
				for ( var i=0;i<5;i++) {
					
					$(arr[i]).val(strValue.wlin_social_econommic_indicator[i].content);
				}
				
				//var protection = $("#protection_level").val();
				if(strValue.wlin_social_econommic_indicator[5].content!=null){
					
					var protectionlevel = $("input[name='content']");
					for(var i=5;i<protectionlevel.length;i++){
					                     //获取所有复选框对象的value属性，然后，用checkArray[i]和他们匹配，如果有，则说明他应被选中
						
							                        //获取复选框的value属性
							                        var checkValue=$(protectionlevel[i]).val();
							                        if(strValue.wlin_social_econommic_indicator[5].content==checkValue){
							                        	$(protectionlevel[i]).attr("checked",true);
							                        }
							                  
					 }
				}
				
				
				
			}
		}
	});
}

function soilgismap() {
	$("#soil_gis_map").empty();
	var filepath = $("#soil_gis_layer").val();
	var map;
	var layer;
	
	dojo.require("dijit.layout.BorderContainer");
	dojo.require("dijit.layout.ContentPane");
	dojo.require("esri.map");
	function init() {

		map = new esri.Map("soil_gis_map",{
			basemap : "streets",
			slider : true
		});
		//var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");
        //map.addLayer(basemap);
		
		var point = new esri.geometry.Point(longitude,
				latitude);
		map.centerAndZoom(point, scale);
		layer = new esri.layers.ArcGISDynamicMapServiceLayer(filepath);

		if (layer.loaded) {
			buildLayerList(layer);
		} else {
			dojo.connect(layer, "onLoad", buildLayerList);
		}

	}
	function buildLayerList(layer) {
		var layerinfos = layer.layerInfos;
		var visible = [];
		var visiblelayer = [];
		if (layerinfos != null && layerinfos.length > 0) {
			
			for (var i = 0, j = layerinfos.length; i < j; i++) {
			var info = layerinfos[i];
			info.defaultVisibility == true;
//			var option = "<option value=" + info.name + ">" + info.name
//					+ "</option>";
//			$("#location_gis_layer").append(option);
			visible.push(info.id);
			}
			
			
			if (visible.length == 0) {
				visible.push(-1);
			}
			layer.setVisibleLayers(visible);
		}
		map.addLayer(layer);
	}
	dojo.ready(init);
}

function socioEconomic_add() {
	var option = {
		dataType : 'json',
		data : {
			wetlandParkId : wetlandParkId
		},
		success : function(value) {
			if (value == true) {
				socioEconomic.html(socioEconomic.text() + successHTML);
				$.gritter.add({
					title : '确认消息',
					text : '恭喜您,信息添加成功！',
					class_name : 'gritter-success'
				});
			} else {
				socioEconomic.html(socioEconomic.text() + warnHTML);
				$.gritter.add({
					title : '确认消息',
					text : '对不起,信息添加失败！',
					class_name : 'gritter-error'
				});
			}
		},
		error : function() {
			socioEconomic.html(socioEconomic.text() + warnHTML);
			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});
		}
	};
	$("#socioEconomic_form").ajaxForm(option).submit();

}
